# -*- coding:utf-8 -*-
import logging
import paramiko
__author__ = "Zhang Dongyu"

class ExecuteScript(object):
    def __init__(self):
        self.script_path = " "
        self.parameter_list = " "
        self.func = "python"

    def update_value(self, key=None, value=None):
        # 更新
        if key == "path":
            self.script_path = value
        elif key == "param":
            self.parameter_list = value
        elif key == "func":
            self.func = value

    def execute_command(self, host=None, port=22, username=None, password=None):
        # 执行
        comm = self.func + self.script_path + self.parameter_list
        logging.info(comm)
        # 创建SSH对象
        ssh = paramiko.SSHClient()
        # 允许链接不在know_hosts的主机，否则可能报错
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        # 连接服务器
        ssh.connect(hostname='192.168.43.140', port=22, username='root', password='123123')

        # stdin：标准输入（就是你输入的命令）；stdout：标准输出（就是命令执行结果）；stderr:标准错误（命令执行过程中如果出错了就把错误打到这里），stdout和stderr仅会输出一个
        stdin, stdout, stderr = ssh.exec_command('df')

        result = stdout.read().decode()

        # 关闭连接
        ssh.close()

